package linearList;

public class JosphProblem {
    public static void main(String[] args) {
        Node<Integer> first = null;
        Node<Integer> pre = null;
        for (int i = 1; i <= 41 ; i++){
            if (i ==1){
                first = new Node<Integer>(i,null);
                pre = first;
                continue;
            }
                Node<Integer> newNode = new Node<Integer>(i, null);
                pre.next = newNode;
                pre = newNode;
                if (i == 41){
                    pre.next = first;
                }
        }

        int count = 0;
        Node n = first;
        Node previous = null;
        while (n != n.next){
            count++;
            if (count == 3){
                previous.next = n.next;
                System.out.println(n.item);
                n=n.next;
                count =0;
            }else {
                previous = n;
                n = n.next;
            }
        }
        System.out.println(n.item);
    }

    private static class Node<T> {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
}
